package cn.net.shuxing.yifu.common.service;

import cn.net.shuxing.yifu.common.bean.User;
import cn.net.shuxing.yifu.common.bean.UserDealHistory;
import cn.net.shuxing.yifu.common.bean.UserRealInfo;
import cn.net.shuxing.yifu.common.constant.*;
import cn.net.shuxing.yifu.common.dto.DealRecordDto;
import cn.net.shuxing.yifu.common.dto.TransferHistoryDto;
import cn.net.shuxing.yifu.common.dto.WalletCoinDealHistoryDto;
import cn.net.shuxing.yifu.common.mapper.UserDealHistoryMapper;
import cn.net.shuxing.yifu.common.util.*;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@Service
public class UserDealHistoryService extends HistoryService<UserDealHistory, UserDealHistoryMapper> {
    @Autowired
    private CoinTypeService coinTypeService;

    @Autowired
    private JedisUtil jedisUtil;

    @Autowired
    private UserService userService;

    @Autowired
    private UserDealHistoryMapper userDealHistoryMapper;

    @Autowired
    private UserRealInfoService userRealInfoService;

    @Autowired
    private UserRoleService userRoleService;
    @Autowired
    private UserCashHistoryService userCashHistoryService;
//    public UserDealHistory insert(Integer userId, Integer coinTypeId, Integer dealType, Integer dealScene, BigDecimal dealAliveCoinCount, BigDecimal singlePrice) {
//        UserDealHistory userDealHistory = new UserDealHistory();
//        userDealHistory.setUserId(userId);
//        userDealHistory.setCoinTypeId(coinTypeId);
//        userDealHistory.setDealType(dealType);
//        userDealHistory.setDealScene(dealScene);
//        userDealHistory.setDealAliveCoinCount(dealAliveCoinCount);
//        userDealHistory.setSinglePrice(singlePrice);
//        insert(userDealHistory);
//        return getByPrimaryKey(userDealHistory.getId());
//    }

    public UserDealHistory insert(Integer userId, Integer coinTypeId, Integer dealType, Integer dealScene, BigDecimal dealAliveCoinCount, BigDecimal singlePrice, int type, String orderKey) {
        UserDealHistory userDealHistory = new UserDealHistory();
        userDealHistory.setUserId(userId);
        userDealHistory.setCoinTypeId(coinTypeId);
        userDealHistory.setDealType(dealType);
        userDealHistory.setDealScene(dealScene);
        userDealHistory.setDealAliveCoinCount(dealAliveCoinCount);
        userDealHistory.setSinglePrice(singlePrice);
        userDealHistory.setType(type);
        userDealHistory.setOrderKey(orderKey);
        insert(userDealHistory);
        return getByPrimaryKey(userDealHistory.getId());
    }

    public List<UserDealHistory> getAllFeeHistory() {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("dealType", DealType.FEE);
//        List<UserDealHistory> userDealHistories = mapper.selectByExample(commonExample);
        List<UserDealHistory> userDealHistoryList = getsByExample(commonExample);
        completeAll(userDealHistoryList);
        return userDealHistoryList;
    }

    public List<UserDealHistory> getFeeHistoryListByDate(LocalDate low, LocalDate high) {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("dealType", DealType.FEE);
        LocalDateTime lowDateTime = LocalDateTimeUtil.createLocalDateTime(low, LocalTimeUtil.createLocalTimeInZero());
        LocalDateTime highDateTime = LocalDateTimeUtil.createLocalDateTime(high, LocalTimeUtil.createLocalTimeInZero());
        commonExample.between("createTime", lowDateTime, highDateTime);
        List<UserDealHistory> userDealHistories = mapper.selectByExample(commonExample);
        completeAll(userDealHistories);
        return userDealHistories;
    }


    @Override
    public List<UserDealHistory> getThisMonth() {
        return super.getThisMonth();
    }

    public LocalDateTime getLastDealTime(Integer userId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        Example.Criteria criteria = example.getCriteria();
        criteria.andLessThan("createTime", LocalDateTime.now());
        example.setOrderByClause("id DESC");
        List<UserDealHistory> userDealHistories = mapper.selectByExampleAndRowBounds(example, new RowBounds(0, 1));
        if (userDealHistories == null || userDealHistories.isEmpty()) {
            return userService.getByPrimaryKey(userId).getCreateTime();
        }
        LocalDateTime createTime = userDealHistories.get(0).getCreateTime();
        return createTime;
    }

    public List<UserDealHistory> getsByUserIdAndLastTime(Integer userId, LocalDateTime lastDateTime) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.between("createTime", lastDateTime, LocalDateTime.now());
        return getsByExample(example);
    }

    public List<UserDealHistory> getsByUserId(Integer userId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        return getsByExample(example);
    }

    public List<UserDealHistory> getsTransferHistoryByUserId(Integer userId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("dealType", DealType.TRANSFER_OUT);
        return getsByExample(example);
    }

    public List<UserDealHistory> getsMemberTransferHistoryByUserId(Integer userId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("type", UserType.MEMBER);
        example.equals("dealType", DealType.TRANSFER_OUT);
        return getsByExample(example);
    }

    public List<UserDealHistory> getHistoryByUserIdAndMonth(Integer userId, Integer month) {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.monthGreaterThan("createTime", month);
        example.equals("userId", userId);
        List<UserDealHistory> userDealHistories = getsByExample(example);
        return userDealHistories;
    }

    public List<UserDealHistory> getHistoryByUserIdAndDay(Integer userId, Integer day) {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.dayGreaterThan("createTime", day);
        example.equals("userId", userId);
        return getsByExample(example);
    }

    public List<UserDealHistory> getHistoryByUserId(Integer userId) {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.equals("userId", userId);
        return getsByExample(example);
    }

    public BigDecimal getThisMonthTransferCount(Integer userId, Integer coinTypeId) {

        User user = userService.getByPrimaryKey(userId);
        BigDecimal totalCount = userDealHistoryMapper.getMonthTransferCount(userId, DealType.TRANSFER_IN, DealType.TRANSFER_OUT, user.getType(), LocalDateTimeUtil.getThisMonthFirstDay());
//        Example example = new Example(UserDealHistory.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("userId", userId);
//        criteria.andEqualTo("coinTypeId", coinTypeId);
//        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getThisMonthFirstDay());
//        example.and(criteria);
//        Example.Criteria criteria1 = example.createCriteria();
//        criteria1.orEqualTo("dealType", DealType.TRANSFER_IN);
//        criteria1.orEqualTo("dealType", DealType.TRANSFER_OUT);
//        User user = userService.getByPrimaryKey(userId);
//        example.and(criteria1);
//        List<UserDealHistory> userDealHistoryList = getsByExample(example);
//        BigDecimal count = new BigDecimal(0);
//        if (userDealHistoryList != null) {
//            for (UserDealHistory userDealHistory : userDealHistoryList) {
//                if (user.getType() == userDealHistory.getType())
//                    count = count.add(userDealHistory.getDealAliveCoinCount());
//            }
//        }
//        return count;
        return totalCount;
    }

    public List<UserDealHistory> getAllNewest(Integer userId, Integer coinTypeId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        example.setOrderByClause("id DESC");
        List<UserDealHistory> userDealHistoryList = getsByExample(example);
        return userDealHistoryList;

    }

    public BigDecimal getThisMonthByUserIdAndCoinTypeId(Integer userId, Integer coinTypeId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
//        User user = userService.getByPrimaryKey(userId);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
//        example.equals("type", user.getType());
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealType", DealType.HANG_BUY);
        criteria.orEqualTo("dealType", DealType.HANG_SALE);
        criteria.orEqualTo("dealType", DealType.SYS_BUY_BACK);
        criteria.orEqualTo("dealType", DealType.FEE);
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getThisMonthFirstDay());
        List<UserDealHistory> userDealHistoryList = getsByExampleWithoutComplete(example);
        BigDecimal count = new BigDecimal(0);
        for (UserDealHistory userDealHistory : userDealHistoryList) {
            count = count.add(userDealHistory.getDealAliveCoinCount());
        }
        return count;
//        return userDealHistoryMapper.getMonthDealCount(userId, DealType.HANG_BUY, DealType.HANG_SALE, DealType.SYS_BUY_BACK,DealType.FEE, LocalDateTimeUtil.getThisMonthFirstDay());
    }

    public BigDecimal getTotalByUserIdAndCoinTypeId(Integer userId, Integer coinTypeId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealType", DealType.HANG_BUY);
        criteria.orEqualTo("dealType", DealType.HANG_SALE);
        criteria.orEqualTo("dealType", DealType.SYS_BUY_BACK);
        criteria.orEqualTo("dealType", DealType.FEE);
        List<UserDealHistory> userDealHistoryList = getsByExampleWithoutComplete(example);
        BigDecimal totalDealCount = new BigDecimal(0);
        if (userDealHistoryList != null && !userDealHistoryList.isEmpty()) {
            for (UserDealHistory userDealHistory : userDealHistoryList) {
                BigDecimal dealAliveCoinCount = userDealHistory.getDealAliveCoinCount();
                totalDealCount = totalDealCount.add(dealAliveCoinCount);
            }
        }
//        BigDecimal totalDealCount = userDealHistoryMapper.getTotalDealCount(userId, DealType.HANG_BUY, DealType.HANG_SALE, DealType.SYS_BUY_BACK,DealType.FEE,coinTypeId);
//
//        if (totalDealCount==null){
//            totalDealCount = new BigDecimal(0);
//        }
        return totalDealCount;
    }

    public BigDecimal getTotalTransferCount(Integer userId, Integer coinTypeId) {

        User user = userService.getByPrimaryKey(userId);
        BigDecimal totalCount = userDealHistoryMapper.getTotalTransferCount(userId, DealType.TRANSFER_IN, DealType.TRANSFER_OUT, user.getType());
//        Example example = new Example(UserDealHistory.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("userId", userId);
//        criteria.andEqualTo("coinTypeId", coinTypeId);
//        example.and(criteria);
//        Example.Criteria criteria1 = example.createCriteria();
//        criteria1.orEqualTo("dealType", DealType.TRANSFER_IN);
//        criteria1.orEqualTo("dealType", DealType.TRANSFER_OUT);
//        example.and(criteria1);
//        List<UserDealHistory> userDealHistoryList = getsByExample(example);
//        BigDecimal count = new BigDecimal(0);
//        User user = userService.getByPrimaryKey(userId);
//        if (userDealHistoryList != null) {
//            for (UserDealHistory userDealHistory : userDealHistoryList) {
//                if (user.getType() == userDealHistory.getType()) {
//                    count = count.add(userDealHistory.getDealAliveCoinCount());
//                }
//            }
//        }
        return totalCount;
    }

    public List<UserDealHistory> getByUserIdAndCoinTypeIdAndDate(Integer userId, Integer coinTypeId, LocalDate startLocaltDate, LocalDate endLocaltDate) {
        LocalDateTime startTime = null;
        if (startLocaltDate != null) {
            startTime = LocalDateTimeUtil.createLocalDateTime(startLocaltDate, LocalTimeUtil.createLocalTimeInZero());
        }
        LocalDateTime endTime = null;
        if (endLocaltDate != null)
            endTime = LocalDateTimeUtil.createLocalDateTime(endLocaltDate, LocalTimeUtil.createLocalTimeInZero());
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        example.between("createTime", startTime, endTime);
        Example.Criteria criteria = example.getCriteria();
        if (startTime != null)
            criteria.andGreaterThanOrEqualTo("createTime", startTime);
        if (endTime != null)
            criteria.andLessThanOrEqualTo("createTime", endTime);
        example.setOrderByClause("id DESC");
        List<UserDealHistory> userDealHistoryList = getsByExample(example);
        return userDealHistoryList;
    }

//    PageHelper.startPage(pageNum, pageSize);

    public List<UserDealHistory> getTransfersByUserIdAndCoinTypeIdAndDate(Integer userId, Integer coinTypeId, LocalDate startLocaltDate, LocalDate endLocaltDate, int pageNum, int pageSize) {
        User user = userService.getByPrimaryKey(userId);
        PageHelper.startPage(pageNum, pageSize);
        List<UserDealHistory> userDealHistories = userDealHistoryMapper.getTransferOutAndTransferInByUserIdAndDate(userId, DealType.TRANSFER_OUT, DealType.TRANSFER_IN,
                user.getType(), startLocaltDate, endLocaltDate);
        if (userDealHistories != null && !userDealHistories.isEmpty()) {
            for (UserDealHistory userDealHistory : userDealHistories) {
                Integer dealType = userDealHistory.getDealType();
                BigDecimal dealAliveCoinCount = userDealHistory.getDealAliveCoinCount();
                String dealTypeName = "";
                if (dealType == DealType.TRANSFER_IN) {
                    dealTypeName = "转账收入";
                }
                if (dealType == DealType.TRANSFER_OUT) {
                    dealTypeName = "转账支出";
                    dealAliveCoinCount = new BigDecimal(0).subtract(dealAliveCoinCount);
                }
                userDealHistory.setDealTypeName(dealTypeName);
                userDealHistory.setDealAliveCoinCount(dealAliveCoinCount);
            }
        }
        return userDealHistories;
    }

    public List<UserDealHistory> getTransfersByUserIdAndCoinTypeIdAndDate(Integer userId, Integer coinTypeId, LocalDate startLocaltDate, LocalDate endLocaltDate) {
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        if (user == null)
            return null;
        Integer type = user.getType();
        LocalDateTime startTime = null;
        if (startLocaltDate != null) {
            startTime = LocalDateTimeUtil.createLocalDateTime(startLocaltDate, LocalTimeUtil.createLocalTimeInZero());
        }
        LocalDateTime endTime = null;
        if (endLocaltDate != null)
            endTime = LocalDateTimeUtil.createLocalDateTime(endLocaltDate, LocalTimeUtil.createLocalTimeInZero());
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        example.equals("type", type);
        Example.Criteria criteria = example.getCriteria();
        if (startTime != null)
            criteria.andGreaterThanOrEqualTo("createTime", startTime);
        if (endTime != null)
            criteria.andLessThanOrEqualTo("createTime", endTime);
        example.and(example.getCriteria());

        criteria = example.createCriteria();
        criteria.orEqualTo("dealType", DealType.TRANSFER_OUT);
        criteria.orEqualTo("dealType", DealType.TRANSFER_IN);
        example.and(criteria);
        example.setOrderByClause("id DESC");
        List<UserDealHistory> userDealHistoryList = getsByExample(example);
        return userDealHistoryList;
    }

    public List<UserDealHistory> getAllDealByUserIdAndCoinTypeId(Integer userId, Integer coinTypeId, Integer pageNum, Integer pageSize) {
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        Integer type = user.getType();
        PageHelper.startPage(pageNum, pageSize);
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        if (type == UserType.SHAREHOLDER) {
            example.equals("type", type);
        } else if (type == UserType.MEMBER) {
            Example.Criteria criteria = example.createCriteria();
            criteria.orEqualTo("type", UserType.NONE);
            criteria.orEqualTo("type", UserType.MEMBER);
            example.and(criteria);
        }
        example.setOrderByClause("id DESC");
        List<UserDealHistory> userDealHistoryList = getsByExample(example);
        return userDealHistoryList;
    }

    public List<UserDealHistory> getAllDealByUserIdAndCoinTypeIdMember(Integer userId, Integer coinTypeId, Integer pageNum, Integer pageSize) {
        User user = userService.getByPrimaryKeyWithoutComplete(userId);
        Integer type = user.getType();
        PageHelper.startPage(pageNum, pageSize);
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("type", UserType.NONE);
        criteria.orEqualTo("type", UserType.MEMBER);
        example.setOrderByClause("id DESC");
        List<UserDealHistory> userDealHistoryList = getsByExample(example);
        return userDealHistoryList;
    }

    public List<UserDealHistory> getsByUserIdNewest(Integer userId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.setOrderByClause("id DESC");
        return getsByExampleWithoutComplete(example);
    }

//    public List<UserDealHistory> getAllTransferByUserIdAndCoinTypeId(Integer userId, Integer coinTypeId) {
//        CommonExample example = new CommonExample(UserDealHistory.class);
//        example.equals("userId",userId);
//        example.equals("coinTypeId",coinTypeId);
//        example.and(example.getCriteria());
//        Example.Criteria criteria = example.createCriteria();
//        criteria.orEqualTo("dealType",DealType.TRANSFER_IN);
//        criteria.orEqualTo("dealType",DealType.TRANSFER_OUT);
//        example.and(criteria);
//        return getsByExampleWithoutComplete(example);
//    }

    public List<UserDealHistory> getHangBuyAndHangSaleByUserId(Integer userId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.and(example.getCriteria());
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealType", DealType.HANG_SALE);
        criteria.orEqualTo("dealType", DealType.HANG_BUY);
        example.and(criteria);
        example.setOrderByClause("id DESC");
        return getsByExample(example);
    }


    @Override
    protected void complete(UserDealHistory bean) {
        if (bean.getDealType() == DealType.HANG_BUY || bean.getDealType() == DealType.HANG_SALE)
            bean.setDealCashCount(bean.getDealAliveCoinCount().multiply(bean.getSinglePrice()));

        Integer dealType = bean.getDealType();
        if (dealType == DealType.FEE)
//                //0在线挂买、1在线挂卖、2股东股东、3股东激活新会员、4会员转会员、5会员转商家、6商家转商家、7商家转会员、8系统回购服务费、9系统回购手续费、10用户购买、11解冻回购手续费
            bean.setDealTypeName("手续费");
        if (dealType == DealType.HANG_BUY)
            bean.setDealTypeName("挂买");
        if (dealType == DealType.HANG_SALE)
            bean.setDealTypeName("挂卖");
        if (dealType == DealType.TRANSFER_OUT)
            bean.setDealTypeName("转账支出");
        if (dealType == DealType.TRANSFER_IN)
            bean.setDealTypeName("转账收入");
        if (dealType == DealType.SYS_BUY_BACK)
            bean.setDealTypeName("系统回购");
        if (dealType == DealType.HANG_SALE_CANSEL)
            bean.setDealTypeName("挂卖撤单退款");
//        Integer dealScene = bean.getDealScene();
//        if (dealType == DealType.FEE) {
////            if (dealScene == DealScene.HANG_SALE){
//                //0在线挂买、1在线挂卖、2股东股东、3股东激活新会员、4会员转会员、5会员转商家、6商家转商家、7商家转会员、8系统回购服务费、9系统回购手续费、10用户购买、11解冻回购手续费
//                bean.setDealTypeName("手续费");
////            }
//
//        } else {
//            //    0-挂买交易、1-挂卖交易、2-转账收入、3-转账支出、4-系统回购、5-手续费、6无'
////            if (dealScene == DealScene.NONE) {
//                if (dealType == DealType.HANG_BUY)
//                    bean.setDealTypeName("挂买");
//                if (dealType == DealType.HANG_SALE)
//                    bean.setDealTypeName("挂卖");
//                if (dealType == DealType.TRANSFER_OUT)
//                    bean.setDealTypeName("转账支出");
//                if (dealType == DealType.TRANSFER_IN)
//                    bean.setDealTypeName("转账收入");
//                if (dealType == DealType.SYS_BUY_BACK)
//                    bean.setDealTypeName("系统回购");
////            }
//        }

    }

    public BigDecimal getTotalDealPrice(Integer userId) {
        List<UserDealHistory> userDealHistories = getsByUserId(userId);
        BigDecimal totalDealPrice = new BigDecimal(0);
        if (userDealHistories == null || userDealHistories.isEmpty()) {
            return totalDealPrice;
        } else {
            for (UserDealHistory userDealHistory : userDealHistories) {
                BigDecimal singlePrice = userDealHistory.getSinglePrice();
                BigDecimal dealAliveCoinCount = userDealHistory.getDealAliveCoinCount();
                BigDecimal multiply = singlePrice.multiply(dealAliveCoinCount);
                totalDealPrice.add(multiply);
            }
            return totalDealPrice;
        }
    }

    public List<UserDealHistory> getsByDealScene(Integer dealScene) {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("dealScene", dealScene);
        return getsByExample(commonExample);
    }

    public List<UserDealHistory> getsByDealSceneAndUserId(Integer dealScene, Integer userId) {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("userId", userId);
        commonExample.equals("dealScene", dealScene);
        return getsByExample(commonExample);
    }

    public List<UserDealHistory> getsByDealTypeAndUserIdAndType(Integer dealType, Integer userId, Integer type) {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("userId", userId);
        commonExample.equals("dealType", dealType);
        commonExample.equals("type", type);
        return getsByExample(commonExample);
    }

    public List<UserDealHistory> getsByDealTypeAndUserId(Integer dealType, Integer userId) {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("userId", userId);
        commonExample.equals("dealType", dealType);
        return getsByExample(commonExample);
    }

    //    public List<UserDealHistory> getsDealHistoryByUserId(Integer userId){
//        Example example = new Example(UserDealHistory.class);
//        Example.Criteria criteria = example.createCriteria();
//        criteria.andEqualTo("userId",userId);
//        return getsByExample(example);
//    }

    public UserDealHistory getsByDealTypeAndOrderKey(Integer dealType, String orderKey) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("dealType", dealType);
        example.equals("orderKey", orderKey);
        return getByExample(example);
    }

    public List<UserDealHistory> getsByDealType(Integer dealType) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("dealType", dealType);
        return getsByExample(example);
    }

    public List<TransferHistoryDto> getUserTransferHistory(Integer userId, Integer userType, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String realName = "";
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        if (userRealInfo != null) {
            realName = userRealInfo.getRealName();
        }
        List<UserDealHistory> transferOut = getsByDealTypeAndUserIdAndType(DealType.TRANSFER_OUT, userId, userType);
        List<TransferHistoryDto> dtos = new ArrayList<>();
        if (transferOut != null && !transferOut.isEmpty()) {
            for (UserDealHistory userDealHistory : transferOut) {
                TransferHistoryDto dto = new TransferHistoryDto();
                dto.setTransferCount(userDealHistory.getDealAliveCoinCount());
                dto.setSenderName(realName);
                String orderKey = userDealHistory.getOrderKey();
                dto.setId(orderKey);
                dto.setCreateTime(userDealHistory.getCreateTime());
                UserDealHistory receiver = getsByDealTypeAndOrderKey(DealType.TRANSFER_IN, orderKey);
                Integer receiverUserId = receiver.getUserId();
                UserRealInfo receiverUserRealInfo = userRealInfoService.getByPrimaryKey(receiverUserId);
                String receiverName = "";
                if (receiverUserRealInfo != null) {
                    receiverName = receiverUserRealInfo.getRealName();
                }
                dto.setReceiverName(receiverName);
                dtos.add(dto);
            }
        }
        return dtos;
    }

    public List<TransferHistoryDto> getUserTransferToShareholder(Integer userId, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        String realName = "";
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        if (userRealInfo != null) {
            realName = userRealInfo.getRealName();
        }
        List<UserDealHistory> transferOut = getsByDealTypeAndUserIdAndType(DealType.TRANSFER_OUT, userId, UserType.SHAREHOLDER);
        List<TransferHistoryDto> dtos = new ArrayList<>();
        if (transferOut != null && !transferOut.isEmpty()) {
            for (UserDealHistory userDealHistory : transferOut) {
                String orderKey = userDealHistory.getOrderKey();
                UserDealHistory receiver = getsByDealTypeAndOrderKey(DealType.TRANSFER_IN, orderKey);
                Integer receiverUserId = receiver.getUserId();
                Boolean aBoolean = userRoleService.checkUserRole(userId, UserType.SHAREHOLDER);
                if (aBoolean) {
                    TransferHistoryDto dto = new TransferHistoryDto();
                    dto.setTransferCount(userDealHistory.getDealAliveCoinCount());
                    dto.setSenderName(realName);
                    dto.setId(orderKey);
                    dto.setCreateTime(userDealHistory.getCreateTime());
                    UserRealInfo receiverUserRealInfo = userRealInfoService.getByPrimaryKey(receiverUserId);
                    String receiverName = "";
                    if (receiverUserRealInfo != null) {
                        receiverName = receiverUserRealInfo.getRealName();
                    }
                    dto.setReceiverName(receiverName);
                    dtos.add(dto);
                }
            }
        }
        return dtos;
    }

    public List<TransferHistoryDto> getUserTransferToShareholder(Integer userId) {
        String realName = "";
        UserRealInfo userRealInfo = userRealInfoService.getByPrimaryKey(userId);
        if (userRealInfo != null) {
            realName = userRealInfo.getRealName();
        }
        List<UserDealHistory> transferOut = getsByDealTypeAndUserIdAndType(DealType.TRANSFER_OUT, userId, UserType.SHAREHOLDER);
        List<TransferHistoryDto> dtos = new ArrayList<>();
        if (transferOut != null && !transferOut.isEmpty()) {
            for (UserDealHistory userDealHistory : transferOut) {
                String orderKey = userDealHistory.getOrderKey();
                UserDealHistory receiver = getsByDealTypeAndOrderKey(DealType.TRANSFER_IN, orderKey);
                Integer receiverUserId = receiver.getUserId();
                Boolean aBoolean = userRoleService.checkUserRole(userId, UserType.SHAREHOLDER);
                if (aBoolean) {
                    TransferHistoryDto dto = new TransferHistoryDto();
                    dto.setTransferCount(userDealHistory.getDealAliveCoinCount());
                    dto.setSenderName(realName);
                    dto.setId(orderKey);
                    dto.setCreateTime(userDealHistory.getCreateTime());
                    UserRealInfo receiverUserRealInfo = userRealInfoService.getByPrimaryKey(receiverUserId);
                    String receiverName = "";
                    if (receiverUserRealInfo != null) {
                        receiverName = receiverUserRealInfo.getRealName();
                    }
                    dto.setReceiverName(receiverName);
                    dtos.add(dto);
                }
            }
        }
        return dtos;
    }

    public List<UserDealHistory> getsDealHistoryByUserId(Integer userId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        Example.Criteria criteria = example.getCriteria();
        ArrayList<Object> objects = new ArrayList<>();
        objects.add(0);
        objects.add(1);
        criteria.andIn("dealType", objects);
        criteria.andIn("dealScene", objects);
        return getsByExample(example);
    }

    public List<UserDealHistory> getHangBuyAndHangSaleAndSystemBuyBackByUserId(Integer userId, Integer
            pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.and(example.getCriteria());
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealScene", DealScene.HANG_SALE);
        criteria.orEqualTo("dealScene", DealScene.HANG_BUY);
        criteria.orEqualTo("dealScene", DealScene.SYSTEM_BUY_BACK);
        example.and(criteria);
        example.setOrderByClause("id DESC");
        List<UserDealHistory> userDealHistoryList = getsByExample(example);
        for (UserDealHistory userDealHistory : userDealHistoryList) {
            userDealHistory.setSimpleName(coinTypeService.getByPrimaryKey(userDealHistory.getCoinTypeId()).getSimpleName());
        }
        return userDealHistoryList;
    }

    public List<UserDealHistory> getHangBuyAndHangSaleAndSystemBuyBackByUserId(Integer userId, Integer coinTypeId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        example.and(example.getCriteria());
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealScene", DealScene.HANG_SALE);
        criteria.orEqualTo("dealScene", DealScene.HANG_BUY);
        criteria.orEqualTo("dealScene", DealScene.SYSTEM_BUY_BACK);
        example.and(criteria);
        example.setOrderByClause("id DESC");
        return getsByExample(example);
    }

    public List<UserDealHistory> getHangBuyAndHangSaleAndSystemBuyBackByUserIdAndDate(Integer userId, LocalDate
            startLocaltDate, LocalDate endLocalDate, Integer pageNum, Integer pageSize) {
        User user = userService.getByPrimaryKey(userId);
        PageHelper.startPage(pageNum, pageSize);
        List<UserDealHistory> userDealHistories = userDealHistoryMapper.getHangBuyAndHangSaleAndSystemBuyBackByUserIdAndDate(userId, DealType.HANG_SALE_CANSEL, DealType.HANG_SALE, DealType.HANG_BUY, DealType.SYS_BUY_BACK, DealType.FEE, user.getType(), startLocaltDate, endLocalDate);
        if (userDealHistories != null && !userDealHistories.isEmpty()) {
//            for (UserDealHistory userDealHistory : userDealHistories) {
//                Integer dealType = userDealHistory.getDealType();
//                BigDecimal dealAliveCoinCount = userDealHistory.getDealAliveCoinCount();
//                if (dealType == DealType.HANG_SALE_CANSEL) {
//                    if (dealType == DealType.FEE || dealType == DealType.SYS_BUY_BACK || dealType == DealType.TRANSFER_OUT || dealType == DealType.HANG_SALE)
//                        userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(dealAliveCoinCount));
//                }
//            }
            for (UserDealHistory userDealHistory : userDealHistories) {
                Integer dealType = userDealHistory.getDealType();
                BigDecimal dealAliveCoinCount = userDealHistory.getDealAliveCoinCount();
                if (dealType == DealType.HANG_BUY) {
                    userDealHistory.setDealTypeName("挂买");
                    dealAliveCoinCount = new BigDecimal(0).subtract(dealAliveCoinCount);
                    userDealHistory.setDealAliveCoinCount(dealAliveCoinCount);
                }

                if (dealType == DealType.HANG_SALE || dealType == DealType.SYS_BUY_BACK) {
                    userDealHistory.setDealTypeName("挂卖");
                    userDealHistory.setDealAliveCoinCount(dealAliveCoinCount);
                }

                if (dealType == DealType.FEE) {
                    dealAliveCoinCount = new BigDecimal(0).subtract(dealAliveCoinCount);
                    userDealHistory.setDealTypeName("手续费");
                    userDealHistory.setDealAliveCoinCount(dealAliveCoinCount);
                }
                if (dealType == DealType.HANG_SALE_CANSEL) {
                    dealAliveCoinCount = new BigDecimal(0).subtract(dealAliveCoinCount);
                    userDealHistory.setDealTypeName("挂卖撤单退款");
                    userDealHistory.setDealAliveCoinCount(dealAliveCoinCount);
                }
            }
        }
        return userDealHistories;
    }

    public List<DealRecordDto> getsByDateAndDealTypeAndUserId(Integer userId, Integer dealType, LocalDate
            startLocaltDate, LocalDate endLocalDate, Integer pageNum, Integer pageSize) {
        User user = userService.getByPrimaryKey(userId);
        ArrayList<DealRecordDto> list = new ArrayList<>();
        PageHelper.startPage(pageNum, pageSize);
        List<UserDealHistory> userDealHistories = userDealHistoryMapper.getsByDateAndDealType(userId, dealType,
                user.getType(), startLocaltDate, endLocalDate);
        if (userDealHistories != null && !userDealHistories.isEmpty()) {
            for (UserDealHistory userDealHistory : userDealHistories) {
                if (dealType == DealType.HANG_BUY) {
                    userDealHistory.setDealTypeName("挂买收入");
                }
                if (dealType == DealType.HANG_SALE || dealType == DealType.SYS_BUY_BACK) {
                    userDealHistory.setDealTypeName("挂卖支出");
                    userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                }
                if (dealType == DealType.TRANSFER_IN) {
                    userDealHistory.setDealTypeName("转账收入");
                }
                if (dealType == DealType.TRANSFER_OUT) {
                    userDealHistory.setDealTypeName("转账支出");
                    userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                }
                DealRecordDto dto = new DealRecordDto();
                dto.setOrderKey(userDealHistory.getOrderKey());
                dto.setCreateTime(userDealHistory.getCreateTime());
                dto.setDealAliveCoinCount(userDealHistory.getDealAliveCoinCount());
                dto.setDealTypeName(userDealHistory.getDealTypeName());
                list.add(dto);
            }
        }
        return list;
    }

    public List<DealRecordDto> getsByDateAndDealTypeIsFee(Integer userId, Integer dealType, LocalDate
            startLocaltDate, LocalDate endLocalDate, Integer pageNum, Integer pageSize) {
        ArrayList<DealRecordDto> list = new ArrayList<>();
        User user = userService.getByPrimaryKey(userId);
        PageHelper.startPage(pageNum, pageSize);
        List<UserDealHistory> userDealHistories = userDealHistoryMapper.getsByUserIdAndDealSceneAndIsFee(userId, dealType,
                user.getType(), startLocaltDate, endLocalDate);
        if (userDealHistories != null && !userDealHistories.isEmpty()) {
            for (UserDealHistory userDealHistory : userDealHistories) {
                if (dealType == DealType.HANG_BUY) {
                    userDealHistory.setDealTypeName("挂买收入");
                }
                if (dealType == DealType.HANG_SALE || dealType == DealType.SYS_BUY_BACK) {
                    userDealHistory.setDealTypeName("挂卖支出");
                    userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                }
                if (dealType == DealType.TRANSFER_IN) {
                    userDealHistory.setDealTypeName("转账收入");
                }
                if (dealType == DealType.TRANSFER_OUT) {
                    userDealHistory.setDealTypeName("转账支出");
                    userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                }
                DealRecordDto dto = new DealRecordDto();
                dto.setOrderKey(userDealHistory.getOrderKey());
                dto.setCreateTime(userDealHistory.getCreateTime());
                dto.setDealAliveCoinCount(userDealHistory.getDealAliveCoinCount());
                dto.setDealTypeName(userDealHistory.getDealTypeName());
                list.add(dto);
            }
        }
        return list;
    }

    public List<DealRecordDto> getAllHistory(Integer userId, Integer dealType, LocalDate
            startLocaltDate, LocalDate endLocalDate, Integer pageNum, Integer pageSize) {
        ArrayList<DealRecordDto> list = new ArrayList<>();
        User user = userService.getByPrimaryKey(userId);
        PageHelper.startPage(pageNum, pageSize);
        List<UserDealHistory> userDealHistories = new ArrayList<>();
        if (dealType == 8) {
            userDealHistories = userDealHistoryMapper.getHistoryNoType(userId, user.getType(), startLocaltDate, endLocalDate);
        } else {
            userDealHistories = userDealHistoryMapper.getAllHistory(userId, dealType, user.getType(), startLocaltDate, endLocalDate);
        }
        if (userDealHistories != null && !userDealHistories.isEmpty()) {
            for (UserDealHistory userDealHistory : userDealHistories) {
                Integer dealScene = userDealHistory.getDealScene();
                dealType = userDealHistory.getDealType();
                if (dealType == DealType.HANG_BUY) {
                    userDealHistory.setDealTypeName("挂买收入");
                }
                if (dealType == DealType.HANG_SALE || dealType == DealType.SYS_BUY_BACK) {
                    userDealHistory.setDealTypeName("挂卖支出");
                    userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                }
                if (dealType == DealType.TRANSFER_IN) {
                    userDealHistory.setDealTypeName("转账收入");
                }
                if (dealType == DealType.TRANSFER_OUT) {
                    userDealHistory.setDealTypeName("转账支出");
                    userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                }
                if (dealType == DealType.FEE) {
                    if (dealScene == DealScene.HANG_BUY) {
                        userDealHistory.setDealTypeName("挂买手续费");
                    }
                    if (dealScene == DealScene.HANG_SALE) {
                        userDealHistory.setDealTypeName("挂卖手续费");
                    }
                    if (dealScene == DealScene.SHAREHOLDER_TRANSFER_SHAREHOLDER) {
                        userDealHistory.setDealTypeName("股东转股东手续费");
                    }
                    if (dealScene == DealScene.SHAREHOLDER_TRANSFER_NEWMEMBER) {
                        userDealHistory.setDealTypeName("激活新钱包手续费");
                    }
                    if (dealScene == DealScene.MEMBER_TRANSFER_MEMBER) {
                        userDealHistory.setDealTypeName("会员转会员手续费");
                    }
                    if (dealScene == DealScene.MEMBER_TRANSFER_SALLER) {
                        userDealHistory.setDealTypeName("会员转商家手续费");
                    }
                    if (dealScene == DealScene.SALLER_TRANSFER_MEMBER) {
                        userDealHistory.setDealTypeName("商家转会员手续费");
                    }
                    if (dealScene == DealScene.SALLER_TRANSFER_SALLER) {
                        userDealHistory.setDealTypeName("商家转商家手续费");
                    }
                    if (dealScene == DealScene.SYSTEM_BUY_BACK) {
                        userDealHistory.setDealTypeName("系统回购服务费");
                    }
                    if (dealScene == DealScene.SYSTEM_BUY_BACK_COMMISSION) {
                        userDealHistory.setDealTypeName("系统回购手续费");
                    }
                    if (dealScene == DealScene.USER_BUY) {
                        userDealHistory.setDealTypeName("用户购买");
                    }
                    if (dealScene == DealScene.FROZEN_BUY_BACK) {
                        userDealHistory.setDealTypeName("解冻手续费");
                    }
                    userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                    DealRecordDto dto = new DealRecordDto();
                    dto.setOrderKey(userDealHistory.getOrderKey());
                    dto.setCreateTime(userDealHistory.getCreateTime());
                    dto.setDealAliveCoinCount(userDealHistory.getDealAliveCoinCount());
                    dto.setDealTypeName(userDealHistory.getDealTypeName());
                    list.add(dto);
                }
            }
        }
        return list;
    }

    public List<DealRecordDto> getsByDateAndDealScene(Integer userId, Integer dealScene, LocalDate
            startLocaltDate, LocalDate endLocalDate, Integer pageNum, Integer pageSize) {
        ArrayList<DealRecordDto> list = new ArrayList<>();
        User user = userService.getByPrimaryKey(userId);
        PageHelper.startPage(pageNum, pageSize);
        List<UserDealHistory> userDealHistories = userDealHistoryMapper.getsByDateAndDealScene(userId, dealScene,
                user.getType(), startLocaltDate, endLocalDate);
        if (userDealHistories != null && !userDealHistories.isEmpty()) {
            for (UserDealHistory userDealHistory : userDealHistories) {
                userDealHistory.setDealAliveCoinCount(new BigDecimal(0).subtract(userDealHistory.getDealAliveCoinCount()));
                if (dealScene == DealScene.HANG_BUY) {
                    userDealHistory.setDealTypeName("挂买手续费");
                }
                if (dealScene == DealScene.HANG_SALE) {
                    userDealHistory.setDealTypeName("挂卖手续费");
                }
                if (dealScene == DealScene.SHAREHOLDER_TRANSFER_NEWMEMBER) {
                    userDealHistory.setDealTypeName("激活新用户手续费");
                }
                if (dealScene == DealScene.SHAREHOLDER_TRANSFER_SHAREHOLDER ||
                        dealScene == DealScene.MEMBER_TRANSFER_MEMBER ||
                        dealScene == DealScene.MEMBER_TRANSFER_SALLER ||
                        dealScene == DealScene.SALLER_TRANSFER_MEMBER ||
                        dealScene == DealScene.SALLER_TRANSFER_SALLER) {
                    userDealHistory.setDealTypeName("转账手续费");
                }
                if (dealScene == DealScene.SYSTEM_BUY_BACK) {
                    userDealHistory.setDealTypeName("系统回购服务费");
                }
                if (dealScene == DealScene.SYSTEM_BUY_BACK_COMMISSION) {
                    userDealHistory.setDealTypeName("系统回购手续费");
                }
                if (dealScene == DealScene.USER_BUY) {
                    userDealHistory.setDealTypeName("用户购买");
                }
                if (dealScene == DealScene.FROZEN_BUY_BACK) {
                    userDealHistory.setDealTypeName("用户解冻手续费");
                }
                DealRecordDto dto = new DealRecordDto();
                dto.setOrderKey(userDealHistory.getOrderKey());
                dto.setCreateTime(userDealHistory.getCreateTime());
                dto.setDealAliveCoinCount(userDealHistory.getDealAliveCoinCount());
                dto.setDealTypeName(userDealHistory.getDealTypeName());
                list.add(dto);
            }
        }
        return list;
    }

    public List<UserDealHistory> getWalletCoinDeal(Integer userId, Integer coinTypeId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("userId", userId);
        example.equals("coinTypeId", coinTypeId);
        example.and(example.getCriteria());
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealScene", DealScene.HANG_SALE);
        criteria.orEqualTo("dealScene", DealScene.HANG_BUY);
        criteria.orEqualTo("dealScene", DealScene.MEMBER_TRANSFER_MEMBER);
        criteria.orEqualTo("dealScene", DealScene.MEMBER_TRANSFER_SALLER);
        criteria.orEqualTo("dealScene", DealScene.SYSTEM_BUY_BACK);
        example.and(criteria);
        example.setOrderByClause("id DESC");
        return getsByExampleWithoutComplete(example);
    }

    public List<WalletCoinDealHistoryDto> getWalletCoinDealHistory(Integer userId,
                                                                   Integer coinTypeId,
                                                                   Integer pageNum,
                                                                   Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<UserDealHistory> historyList = getWalletCoinDeal(userId, coinTypeId);
        ArrayList<WalletCoinDealHistoryDto> walletCoinDealHistoryDtos = new ArrayList<>();

        if (historyList != null && !historyList.isEmpty()) {
            for (UserDealHistory userDealHistory : historyList) {
                WalletCoinDealHistoryDto listDto = new WalletCoinDealHistoryDto();
                listDto.setCreateTime(userDealHistory.getCreateTime());
                Integer dealType = userDealHistory.getDealType();
                if (dealType == DealType.HANG_SALE)
                    listDto.setDealTypeName("挂卖");
                if (dealType == DealType.HANG_BUY)
                    listDto.setDealTypeName("挂买");
                if (dealType == DealType.TRANSFER_IN)
                    listDto.setDealTypeName("转账收入");
                if (dealType == DealType.TRANSFER_OUT)
                    listDto.setDealTypeName("转账支出");
                if (dealType == DealType.FEE)
                    listDto.setDealTypeName("转账手续费");
                BigDecimal dealCashCount = userDealHistory.getDealCashCount();
                listDto.setDealCount(dealCashCount);
                //获取公价
                BigDecimal openPrice = new BigDecimal(jedisUtil.get(RedisKeyCreator.openPrice(userDealHistory.getCoinTypeId())));
                listDto.setConvertCount(dealCashCount.multiply(openPrice));
                walletCoinDealHistoryDtos.add(listDto);
            }
        }

        return walletCoinDealHistoryDtos;
    }


    public List<UserDealHistory> getsYestodayByCoinCount(Integer coinTypeId) {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.equals("coinTypeId", coinTypeId);
        LocalDateTime todayZone = LocalDateTimeUtil.createLocalDateTime(LocalDate.now(), LocalTimeUtil.createLocalTimeInZero());
        LocalDateTime yestodayZone = todayZone.plusDays(-1L);
        example.between("createTime", yestodayZone, todayZone);
        return getsByExampleWithoutComplete(example);
    }

    public BigDecimal getHangSaleMonth() {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.equals("dealScene", DealScene.HANG_SALE);
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getThisMonthFirstDay());
        List<UserDealHistory> dealHistories = getsByExampleWithoutComplete(example);
        BigDecimal month = new BigDecimal(0);
        for (UserDealHistory dealHistory : dealHistories) {
            month = month.add(dealHistory.getDealAliveCoinCount());
        }
        return month;
    }

    public BigDecimal getHangSaleTotal() {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("dealScene", DealScene.HANG_SALE);
        List<UserDealHistory> dealHistories = getsByExample(commonExample);
        BigDecimal total = new BigDecimal(0);
        for (UserDealHistory dealHistory : dealHistories) {
            BigDecimal dealAliveCoinCount = dealHistory.getDealAliveCoinCount();
            total = total.add(dealAliveCoinCount);
        }
        return total;
    }

    public List<UserDealHistory> getByTodayAndDealScene(int dealScene) {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.equals("dealScene", dealScene);
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTime.now());
        example.and(criteria);
        List<UserDealHistory> dealHistories = getsByExample(example);
        return dealHistories;
    }

    public List<UserDealHistory> getSurveyDealHistory() {
        CommonExample example = new CommonExample(UserDealHistory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealScene", DealScene.HANG_BUY);
        criteria.orEqualTo("dealScene", DealScene.HANG_SALE);
        criteria.orEqualTo("dealScene", DealScene.USER_BUY);
        example.and(criteria);
        return mapper.selectByExample(example);
    }

    public List<UserDealHistory> getTodaySurveyDealHistory() {
        TimeExample example = new TimeExample(UserDealHistory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("dealScene", DealScene.HANG_BUY);
        criteria.orEqualTo("dealScene", DealScene.HANG_SALE);
        criteria.orEqualTo("dealScene", DealScene.USER_BUY);
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTime.now());
        example.and(criteria);
        return mapper.selectByExample(example);
    }

    public Integer getTotalCount() {
        CommonExample example = new CommonExample(UserDealHistory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNotNull("id");
        return mapper.selectCountByExample(example);
    }


    public Integer getTodayCount() {
        TimeExample example = new TimeExample(UserDealHistory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIsNotNull("id");
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTime.now());
        return mapper.selectCountByExample(example);
    }

    public BigDecimal getHangBuyTotal() {
        CommonExample commonExample = new CommonExample(UserDealHistory.class);
        commonExample.equals("dealScene", DealScene.HANG_BUY);
        List<UserDealHistory> dealHistories = getsByExample(commonExample);
        BigDecimal total = new BigDecimal(0);
        for (UserDealHistory dealHistory : dealHistories) {
            BigDecimal dealAliveCoinCount = dealHistory.getDealAliveCoinCount();
            total = total.add(dealAliveCoinCount);
        }
        return total;
    }

    public BigDecimal getHangBuyMonth() {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.equals("dealScene", DealScene.HANG_BUY);
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getThisMonthFirstDay());
        List<UserDealHistory> dealHistories = getsByExample(example);
        BigDecimal month = new BigDecimal(0);
        for (UserDealHistory dealHistory : dealHistories) {
            month = month.add(dealHistory.getDealAliveCoinCount());
        }
        return month;
    }

    public List<UserDealHistory> getDealHistoryByDealScene(int dealScene) {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.equals("dealScene", dealScene);
        Example.Criteria criteria = example.getCriteria();
        criteria.andGreaterThanOrEqualTo("createTime", LocalDateTimeUtil.getThisMonthFirstDay());
        List<UserDealHistory> dealHistories = getsByExample(example);
        return dealHistories;
    }

    public List<UserDealHistory> getAllSystemBuyBack(Integer coinTypeId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("coinTypeId", coinTypeId);
        example.equals("dealScene", DealScene.SYSTEM_BUY_BACK);
        return getsByExampleWithoutComplete(example);
    }

    public List<UserDealHistory> getsByCoinTypeId(Integer coinTypeId) {
        CommonExample example = new CommonExample(UserDealHistory.class);
        example.equals("coinTypeId", coinTypeId);
        return getsByExampleWithoutComplete(example);
    }

    public List<UserDealHistory> getsThisDayByCoinTypeId(Integer coinTypeId) {
        TimeExample example = new TimeExample(UserDealHistory.class);
        example.equals("coinTypeId", coinTypeId);
        LocalDateTime localDateTime = LocalDateTimeUtil.createLocalDateTime(LocalDate.now(), LocalTimeUtil.createLocalTimeInZero());
        example.timeGreaterThan("createTime", localDateTime);
        return getsByExampleWithoutComplete(example);
    }

    public String createOrderNo(Integer length) {

        StringBuffer total = new StringBuffer("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ");
        StringBuffer orderNo = new StringBuffer();
        Random r = new Random();
        int range = total.length();
        //循环字符长度
        for (int i = 0; i < length; i++) {
            //生成随机字符
            orderNo.append(total.charAt(r.nextInt(range)));
        }
        return orderNo.toString();
    }

    public List<UserDealHistory> getKLineDealData(Integer coinTypeId, LocalDateTime time, long minuteCount) {
        LocalDateTime endTime = time.plusMinutes(0 - minuteCount);
        TimeExample timeExample = new TimeExample(UserDealHistory.class);
        timeExample.equals("coinTypeId", coinTypeId);
        timeExample.between("createTime", time, endTime);
        timeExample.setOrderByClause("id ASC");
        List<UserDealHistory> dealHistoryList = getsByExample(timeExample);
        return dealHistoryList;
    }

}